package top.zywork.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.zywork.annotation.AppAuthFilter;
import top.zywork.annotation.SysLog;
import top.zywork.annotation.XssFilter;
import top.zywork.common.TransactionNoGenerator;
import top.zywork.constant.CommonConstant;
import top.zywork.dos.SchoolOrderDO;
import top.zywork.dto.OrderPayDTO;
import top.zywork.dto.RefundOrderDTO;
import top.zywork.enums.PayOrderEnum;
import top.zywork.enums.PayStatusEnum;
import top.zywork.enums.RefundOrderEnum;
import top.zywork.security.JwtUser;
import top.zywork.security.SecurityUtils;
import top.zywork.service.GoodsOrderService;
import top.zywork.service.PayNotifyService;
import top.zywork.service.PayService;
import top.zywork.service.SchoolOrderService;
import top.zywork.strategy.order.OrderContext;
import top.zywork.vo.ResponseStatusVO;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>微信支付控制器</p>
 * 创建于2019-07-19<br/>
 *
 * @author 王振宇
 * @version 1.0
 */
@RestController
@RequestMapping("/wx-pay")
@Slf4j
@XssFilter(ignoreUrls = {"/wx-order/goods-order/wx-order-notify/**"})
@AppAuthFilter(ignoreUrls = {"/wx-order/goods-order/wx-order-notify/**"})
public class WeixinPayController extends BaseController {

    private PayService payService;


    private PayNotifyService payNotifyService;

    @Autowired
    private OrderContext orderContext;

    /**
     * 商城订单小程序发起支付
     * @param orderId 订单号，之间用,隔开 2020101514283119657184441,2020101514283119657184442
     * @return
     */
    @GetMapping("goods-order/xcx-pay-request/{type}/{orderId}")
    @SysLog(description = "商城订单小程序发起支付请求")
    public ResponseStatusVO goodsOrderWxWcxPayRequest(@PathVariable("type") String type, @PathVariable("orderId") String orderId) {
        // 检查已开启支付的服务
        PayOrderEnum anEnum = PayOrderEnum.getEnum(type);
        if(anEnum==null){
            return ResponseStatusVO.error("支付通道未开启",null);
        }
        JwtUser jwtUser = SecurityUtils.getJwtUser();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("userId", jwtUser.getUserId());
        jsonObject.put("orderIdAndNos", orderId);
        OrderPayDTO orderPayDTO = orderContext.OrderValidation(type, orderId);
//        return payService.payByXcxWxpay(jwtUser.getUsername(), TransactionNoGenerator.generateOrderNo(), "127.0.0.1",
//                "商城订单支付", jsonObject.toJSONString(), orderPayDTO.getTotalPay().multiply(new BigDecimal(100)).intValue(), orderPayDTO.getPayNotifyUrl());

        return payService.payByXcxWxpay(jwtUser.getUsername(), TransactionNoGenerator.generateOrderNo(), "127.0.0.1",
                "商城订单支付", jsonObject.toJSONString(), orderPayDTO.getTotalPay().intValue(), orderPayDTO.getPayNotifyUrl());
    }

    /**
     * 订单统一发起退款也就是取消订单
     * @param orderId 订单号，之间用,隔开 2020101514283119657184441,2020101514283119657184442
     * @return
     */
    @GetMapping("goods-order/xcx-refund-request/{type}/{orderId}")
    @SysLog(description = "订单发起取消订单")
    public ResponseStatusVO goodsOrderWxWcxRefundRequest(@PathVariable("type") String type, @PathVariable("orderId") String orderId) {
        // 检查已开启支付的服务
        RefundOrderEnum anEnum = RefundOrderEnum.getEnum(type);
        if(anEnum==null){
            return ResponseStatusVO.error("退款通道未开启",null);
        }
        RefundOrderDTO refundOrderDTO = orderContext.refundValidation(type, orderId);
        return payService.refundByXcxWxpay(refundOrderDTO);
    }

    /**
     * 商城微信支付回调
     * @param request
     * @param response
     */
    @PostMapping("goods-order/wx-pay-notify/{type}")
    public void wxGoodsOrderPayNotify(@PathVariable("type") String type,HttpServletRequest request, HttpServletResponse response) {
        payService.payByWxNotify(type,request, response, payNotifyService);
    }

    /**
     * 商城微信退款回调
     *
     * @param request
     * @param response
     */
    @PostMapping("goods-order/wx-refund-notify/{type}")
    public void wxGoodsOrderRefundNotify(@PathVariable("type") String type,HttpServletRequest request, HttpServletResponse response) {
        payService.refundByWxNotify(type,request, response, payNotifyService);
    }

    @Autowired
    public void setPayService(PayService payService) {
        this.payService = payService;
    }

    @Autowired
    public void setPayNotifyService(PayNotifyService payNotifyService) {
        this.payNotifyService = payNotifyService;
    }
}
